import API from '../../api/api.js'
import Dialog from '../../lib/vant-weapp/dialog/dialog';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    curId: "",
    screenHeight: app.globalData.screenHeight
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '文章详情与评论',
    })
    this.data.curId = options.id
    if("mode" in options){
      this.setData({
        mode:options.mode
      })
    }
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  onEditorReady() {
    const that = this
    API.getDataById('wenzhangbiao', this.data.curId).then(res => {
      let data = res[0]
      that.createSelectorQuery().select('#readonlyeditor').context(function (res) {
        that.editorCtx = res.context
        that.editorCtx.setContents({
          html: data.neiRong
        })
      }).exec()
      this.setData({
        data: res[0]
      })
    })


  },
  format: function (e) {
    let {
      name,
      value
    } = e.target.dataset
    if (!name) return
    this.editorCtx.format(name, value)

  },

  onWriteCommentFunc: function (e) {
    
    wx.setStorageSync('currWenZhangComment', e.currentTarget.dataset.value)
    wx.navigateTo({
      url: "/pages/comment/comment?mode=comment"
    })
  },

  onTianJiaBiYouFunc: function (e) {
    Dialog.confirm({
        title: '添加笔友',
        message: '请确定是否添加笔友，添加后可以在笔友社查看笔友。',
      })
      .then(() => {
        wx.showLoading({
          title: '加载中...',
          mask:true
        })
        let item = e.currentTarget.dataset.value
        let userInfo = wx.getStorageSync('userInfo')
        let flag = false
        userInfo.biYouLieBiao.forEach(value =>{
          if(value.openid == item.openid){
            flag = true
          }
        })
        if(flag == true){
          wx.showToast({
            title: '你已经添加过此用户。',
            icon:"none"
          })
          return
        }
        if (item._openid == userInfo._openid) {
          wx.showToast({
            title: '不能添加自己。',
            icon: 'none'
          })
          return
        }
        let biyoudata = {
          openid: item._openid,
          biMing: item.biMing
        }
        if (userInfo.biYouLieBiao.length < userInfo.biYouShuLiang) {
          userInfo.biYouLieBiao.unshift(biyoudata)
          API.updateBiYouList(userInfo._openid, userInfo.biYouLieBiao).then(res => {
            wx.setStorageSync('userInfo', userInfo)
            wx.hideLoading()
            wx.showToast({
              title: '添加笔友【' + item.biMing + '】成功',
              icon: "none"
            })
          })
        } else {
          wx.hideLoading()
          wx.showToast({
            title: '笔友宜精不宜多，暂时不能添加更多的笔友，请见谅。',
            icon: "none"
          })
        }
        
      })
      .catch(res => {
        console.error(res)
        wx.showToast({
          title: '已经取消添加笔友。',
          icon: "none"
        })
      })

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let shareimages = app.globalData.shareimages
    let index = Math.floor(Math.random() * 5)
    return {
      title: '一个真正匿名的心情小程序',
      imageUrl: shareimages[index],
      path: '/pages/index/index'
    }
  },
  onShareTimeline: function () {
let shareimages = app.globalData.shareimages
    let index = Math.floor(Math.random() * 5)
    return {
      title: '一个真正匿名的心情小程序',
      imageUrl: shareimages[index]
    }
  }
})